package com.health.viewscontrols;

import java.util.ArrayList;
import java.util.List;

import com.blankj.utilcode.util.AdaptScreenUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ServiceUtils;
import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.jtys.JTYSDA;
import com.health.fragment.jtys.JTYSHome;
import com.health.fragment.jtys.JTYSJD;
import com.health.fragment.jtys.JTYSSettings;
import com.health.fragment.jtys.SatisfactionFragment;
import com.health.fragment.jtys.TJFragment;
import com.health.idcard.service.USBIdCardService;
import com.health.tools.GlobalContsJTYS;
import com.health.tablebean.JTYSDoctorContInfoBean;
import com.health.tools.IDCardTool.IDCardConnectListener;
import com.health.tools.PinyinUtils;
import com.health.tools.SPUtil;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import net.tsz.afinal.db.sqlite.DbModel;

/**
 * 家庭医生
 * 
 * @author nova
 *
 */
public class MainActivity_Menu_JTYS extends NovaFragmentActivity implements OnClickListener, IDCardConnectListener {

	private static final String TAG = "MainActivity_Menu_JTYS";
	public static ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments = null;
	private ImageView backBtn;
	private FragmentAdapter adapter;
	private LinearLayout ll_exit;
	RelativeLayout rev_title;
	public RadioGroup main_tab_group;
	Fragment jdFragment = null;
	private String from = "";
	JTYSHome homeFragment;

	SatisfactionFragment satisfactionFragment;
	TJFragment tjFragment;

	@Override
	public Resources getResources() {
		return AdaptScreenUtils.adaptWidth(super.getResources(), 840);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.jtys_main);
		initView();
		addListener();
		NovaApplication.dbManger.addColumnByBySQL("OLDER_SELFCAREABILITY", "IS_UPDATE");
		NovaApplication.dbManger.addColumnByBySQL("OLD_MEDICINE_CN", "IS_UPDATE");

		NovaApplication.dkType = SPStaticUtils.getString("sp_dkType", "内置读卡");

		if (NovaApplication.idCardTool != null) {
			NovaApplication.idCardTool.setListener(this);
		}
		Log.e(TAG, NovaApplication.getImei() + "\n" + NovaApplication.getXzqhNo() + "\n"
				+ SPUtil.getIsShiYong(MainActivity_Menu_JTYS.this));

		// if (NovaApplication.bloothTool != null) {
		// NovaApplication.bloothTool.scanLeDevice(true);
		// }
		if (getIntent().hasExtra("from")) {
			from = getIntent().getStringExtra("from");
		}
		List<JTYSDoctorContInfoBean> listsize = NovaApplication.fdbmanger.findAllByWhere(JTYSDoctorContInfoBean.class,
				"officeId='" + NovaApplication.getJgbh() + "'");
		if (listsize.size() == 0) {
			// 需要下载服务项目包，进行提示
//			new AlertDialog.Builder(MainActivity_Menu_JTYS.this).setTitle(getString(R.string.alert_dialog_titile))// 设置对话框标题
//					.setMessage(getString(R.string.main_activity_message_1))// 设置显示的内容
//					.setPositiveButton(getString(R.string.enter), new DialogInterface.OnClickListener() {// 添加确定按钮
//						@Override
//						public void onClick(DialogInterface dialog, int which) {// 确定按钮的响应事件
//							// TODO Auto-generated method stub
//							startActivity(new Intent(MainActivity_Menu_JTYS.this, DataSynchronizationMainActivity.class));
//						}
//					}).setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {// 添加返回按钮
//						@Override
//						public void onClick(DialogInterface dialog, int which) {// 响应事件
//							// TODO Auto-generated method stub
//						}
//					}).show();// 在按键响应事件中显示此对话框
		}

		try {
			ServiceUtils.startService(USBIdCardService.class);
//			ArchiveUpload.uploadEvaluate();
//			ArchiveUploadWebService uploadWebService = new ArchiveUploadWebService();
//			uploadWebService.uploadWebServiceArchive(this);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		// if (NovaApplication.idCardTool != null) {
		// NovaApplication.idCardTool.removeListener();
		// }
		super.onPause();
		from = "";
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.e(TAG, "onResume()");
		Log.e(TAG, "onResume--" + from);
		if (TextUtils.isEmpty(from)) {
			if (NovaApplication.idCardTool != null) {
				NovaApplication.idCardTool.setResume();
				NovaApplication.idCardTool.setListener(this);
				NovaApplication.idCardTool.startScanReceive();
			}
		}
	}

	private void initView() {
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton rb_jtys_home = (RadioButton) findViewById(R.id.jtys_home);
		RadioButton rb_jtys_jd = (RadioButton) findViewById(R.id.jtys_jd);
		RadioButton rb_jtys_da = (RadioButton) findViewById(R.id.jtys_da);
//		RadioButton rb_jtys_health = (RadioButton) findViewById(R.id.jtys_health);
		RadioButton rb_jtys_satisfaction = (RadioButton) findViewById(R.id.jtys_satisfaction);
		RadioButton rb_jtys_settings = (RadioButton) findViewById(R.id.jtys_settings);
		ll_exit = (LinearLayout) findViewById(R.id.ll_exit);
		rev_title = (RelativeLayout) findViewById(R.id.rev_title);
		main_tab_group = (RadioGroup) findViewById(R.id.main_tab_group);

		WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
		int mScreenWidth = wm.getDefaultDisplay().getWidth();
		int mScreenHeight = wm.getDefaultDisplay().getHeight();
		LayoutParams layoutParams = rev_title.getLayoutParams();
		layoutParams.width = mScreenWidth;
		layoutParams.height = (int) (0.08 * mScreenHeight);
		rev_title.setLayoutParams(layoutParams);
		LayoutParams layoutParams2 = main_tab_group.getLayoutParams();
		layoutParams2.width = (int) (0.15 * mScreenWidth);
		layoutParams2.height = (int) (0.85 * mScreenHeight);
		main_tab_group.setLayoutParams(layoutParams2);

		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(rb_jtys_home);
		radioButtons.add(rb_jtys_jd);
		radioButtons.add(rb_jtys_da);
//		radioButtons.add(rb_jtys_health);
		radioButtons.add(rb_jtys_satisfaction);
		radioButtons.add(rb_jtys_settings);

		rb_jtys_home.setOnClickListener(this);
		rb_jtys_jd.setOnClickListener(this);
		rb_jtys_da.setOnClickListener(this);
//		rb_jtys_health.setOnClickListener(this);
		rb_jtys_satisfaction.setOnClickListener(this);
		rb_jtys_settings.setOnClickListener(this);

		homeFragment = new JTYSHome();
		jdFragment = new JTYSJD();

		Fragment daFragment = new JTYSDA();
		//档案界面复用，需要传值区分

		Bundle bundle = new Bundle();
		bundle.putString(GlobalContsJTYS.FRAGMENT_MEMBER_INTENT_ID, GlobalContsJTYS.FRAGMENT_MEMBER_INTENT_ID_MAIN);
		daFragment.setArguments(bundle);


//		Fragment healthFragment = new AdminDocterUpload();
		satisfactionFragment = new SatisfactionFragment();
		tjFragment = new TJFragment();
		Fragment settingsFragment = new JTYSSettings();
		fragments = new ArrayList<Fragment>();
		fragments.add(homeFragment);
		fragments.add(jdFragment);
		fragments.add(daFragment);
//		fragments.add(healthFragment);
		fragments.add(tjFragment);
		fragments.add(settingsFragment);
//		fragments.add(satisfactionFragment);

		adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);
		viewPager.setOffscreenPageLimit(1);
		adapter.notifyDataSetChanged();

		backBtn = (ImageView) findViewById(R.id.img_back);
		backBtn.setOnClickListener(this);
		ll_exit.setOnClickListener(this);
//		setChecked(0);
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {

				radioButtons.get(position).setChecked(true);
					
//				setChecked(position);
//				if (position == 3) {
//					satisfactionFragment.refresh();
//				}
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}

		});

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.jtys_home:
			viewPager.setCurrentItem(0, false);
//			setChecked(0);
			break;
		case R.id.jtys_jd:
			viewPager.setCurrentItem(1, false);
//			setChecked(1);
			break;
		case R.id.jtys_da:

			viewPager.setCurrentItem(2, false);
//			setChecked(2);
			break;
		/*case R.id.jtys_health:
			viewPager.setCurrentItem(3, false);
			setChecked(3);
			break;*/
		case R.id.jtys_satisfaction:
			viewPager.setCurrentItem(3, false);
//			setChecked(3);
			homeFragment.myd = "3";
			break;
		case R.id.jtys_settings:
			viewPager.setCurrentItem(4, false);
//			setChecked(4);
			break;
		case R.id.btn_back:
			finish();
			break;
		case R.id.ll_exit:
			finish();
			break;

		default:
			break;
		}

	}

	/*private void setChecked(int pos) {
		for (RadioButton rbtn : radioButtons) {
			rbtn.setBackground(null);
		}
		radioButtons.get(pos).setBackgroundResource(getResources().getColor(R.color.white));

	}*/

	@Override
	public void onIDCardConnect() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onIDDataReceived(String data) {
		// TODO Auto-generated method stub
		((JTYSJD) jdFragment).setIdInfo(data);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ServiceUtils.stopService(USBIdCardService.class);
	}
}
